package com.tianhua.javs.client.bean;

import com.tianhua.javs.client.enums.BusinessKeyWardEnum;
import com.tianhua.javs.client.model.AbstractClassModel;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.script.ScriptEngine;
import java.util.List;

/**
 * Description:
 * date: 2022/2/12
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Data
@Builder
public class ScriptVarMappingBean {



    /**
     * javs脚本中的关键字
     */
    private String javsVarCode;

    /**
     * javs脚本中关键字对应的Java代码段
     */
    private String javaCode;

    /**
     * 变量在java中的类型
     * List
     * Map
     * XxxBO
     * Integer
     * Long
     */
    private String varClassName;


    /**
     * 变量值标签
     * get/set
     */
    private String tag;

    /**
     * 关联的类
     * 用来区分存在多个变量值的情况下用这个来判断属于
     * 哪个类的资源，防止重复覆盖
     */
    private String referClassName;



    /**
     * 变量指向的具体对象
     */
    private AbstractClassModel referClassModel;


    /**
     * 变量声明的范型类型
     * List<String>    ---> String
     * List    ---> Object ---> null
     * Map<String,String> ----> String,String
     * //带有嵌套场景的范型
     * Map<String,List<String>> ----> String,List<String>
     *
     */
    private List<String> varGenerateClassType;



    public static ScriptVarMappingBean getInstance(String varCode, String varRealCode){
        return  ScriptVarMappingBean.builder()
                .javsVarCode(varCode)
                .javaCode(varRealCode).build();
    }

    public static ScriptVarMappingBean getInstance(String varCode, String varRealCode, String varClassName){
        return  ScriptVarMappingBean.builder()
                .javsVarCode(varCode)
                .varClassName(varClassName)
                .javaCode(varRealCode).build();
    }

    public static  ScriptVarMappingBean build(JavsParamBean javsParamBean){
        return  ScriptVarMappingBean.builder()
                .javsVarCode(javsParamBean.getJavsParamName())
                .varClassName(javsParamBean.getParamType())
                .javaCode(javsParamBean.getOriginParamName()).build();
    }


    /**
     * 将变量映射模型转换为参数模型
     * @return
     */
    public JavsParamBean convertParamBean(){
        JavsParamBean javsParamBean = JavsParamBean.ofInstance(this.javsVarCode);
        if(this.getReferClassModel() != null){
            javsParamBean.setParamType(this.getReferClassModel().getClassName());
        }
        javsParamBean.setReferClass(this.referClassModel);
        if(StringUtils.isNotEmpty(this.javaCode)){
            javsParamBean.setOriginParamName(this.javaCode);
        }else if(BusinessKeyWardEnum.isJavsModel(this.javsVarCode) && StringUtils.isNotEmpty(this.varClassName)){
            String javaParamName = this.varClassName.substring(0,1).toLowerCase() + this.varClassName.substring(1);
            javsParamBean.setOriginParamName(javaParamName);
        }else {
            javsParamBean.setOriginParamName(this.javsVarCode);
        }

        return javsParamBean;
    }

}
